package com.sunny.ynhz.ui.report;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.ChooseSubmitObjectAdapter;
import com.sunny.ynhz.bean.RiverChiefBeanMsg;
import com.sunny.ynhz.model.IProblemTypeResultView;
import com.sunny.ynhz.model.RiverChiefModel;
import com.sunny.ynhz.ui.base.BaseFragment;
import com.sunny.ynhz.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static android.app.Activity.RESULT_OK;

/**
 * Created by huhuan123 on 2018/9/16.
 *
 * @describe TODO
 */

public class RiverChiefFragment extends BaseFragment implements IProblemTypeResultView<RiverChiefBeanMsg> {
    @BindView(R.id.lv_current_river)
    RecyclerView rv_current_river;
    @BindView(R.id.ed_search_river)
    EditText editText;
    @BindView(R.id.ll_choose)
    LinearLayout ll_choose;
    @BindView(R.id.tv_choose_area)
    TextView tv_choose_area;

    List<RiverChiefBeanMsg.DateBean> mList = new ArrayList<>();

    List<RiverChiefBeanMsg.DateBean> mAllList = new ArrayList<>();
    private ChooseSubmitObjectAdapter submitObjectAdapter;
    private RiverChiefModel riverChiefModel;

    public static RiverChiefFragment getRiverChiefFragment() {
        return new RiverChiefFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.rv_river_msg;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
        editText.setHint("请输入姓名");
        String userType = SPUtil.get("userType", "").toString();
        if (userType.equals("1") || userType.equals("2") || userType.equals("3") || userType.equals("4") || userType.equals("5") || userType.equals("6")) {
            ll_choose.setVisibility(View.VISIBLE);
        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_current_river.setLayoutManager(linearLayoutManager);
        submitObjectAdapter = new ChooseSubmitObjectAdapter(mList);
        rv_current_river.setAdapter(submitObjectAdapter);
        riverChiefModel = new RiverChiefModel(getContext(), this);
        riverChiefModel.getRiverChiefData(2, true);
        submitObjectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RiverChiefBeanMsg.DateBean dateBean = mList.get(position);
                String name = dateBean.getNAME();
                String id = dateBean.getID();
                Intent intent = new Intent();
                intent.putExtra("id", id);
                intent.putExtra("name", name);
                getActivity().setResult(500, intent);
                getActivity().finish();

            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String name = charSequence.toString();
                if (TextUtils.isEmpty(name) && TextUtils.isEmpty(tv_choose_area.getTag().toString())) {
                    mList.clear();
                    mList.addAll(mAllList);

                } else {
                    List<RiverChiefBeanMsg.DateBean> dateBeans = riverChiefModel.serachPersion(mAllList, name, tv_choose_area.getTag().toString());
                    mList.clear();
                    mList.addAll(dateBeans);
                }
                submitObjectAdapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        ll_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), ChooseAreaActivity.class);
                intent.putExtra("type", 1);
                startActivityForResult(intent, 500);
            }
        });
    }


    @Override
    public void onSuccess(RiverChiefBeanMsg riverChiefBeanMsg) {
        loadSuccess();
        mList.addAll(riverChiefBeanMsg.getDate());
        submitObjectAdapter.notifyDataSetChanged();
        mAllList.addAll(riverChiefBeanMsg.getDate());


    }

    @Override
    public void onCache() {

    }

    @Override
    public void onFail() {
        loadFail();

    }

    @Override
    public void onStartLoad() {
        startLoad();
    }

    @Override
    public void resLoad() {
        super.resLoad();
        riverChiefModel.getRiverChiefData(2, true);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 500 && resultCode == RESULT_OK) {
            String areaCode = data.getStringExtra("areaCode");
            String name = data.getStringExtra("areaName");
            tv_choose_area.setTag(areaCode);
            tv_choose_area.setText(name);
            if (TextUtils.isEmpty(editText.getText().toString()) && TextUtils.isEmpty(tv_choose_area.getTag().toString())) {
                mList.clear();
                mList.addAll(mAllList);
            } else {
                List<RiverChiefBeanMsg.DateBean> dateBeans = riverChiefModel.serachPersion(mAllList, editText.getText().toString(), tv_choose_area.getTag().toString());
                mList.clear();
                mList.addAll(dateBeans);
            }
            submitObjectAdapter.notifyDataSetChanged();
        }
    }
}
